var SettingsCtrl = dojo.declare(null, {
	//constructor/destructor
	constructor : settingsCtrl_contructor,
	destroy : settingsCtrl_destructor,
	
	//methods
	init : settingsCtrl_init,
	processSubmit : settingsCtrl_processSubmit
});

function validateOldPass(){
	var password = $(".pp_pic_holder #form_changepass #input_oldpass").val();
	return isValidPassword(password);
}

function validateNewPass(){
	var password = $(".pp_pic_holder #form_changepass #input_newpass").val();
	return isValidPassword(password);
}

function validateRetypePass(){
	var pass1 = $(".pp_pic_holder #form_changepass #input_newpass").val();
	var pass2 = $(".pp_pic_holder #form_changepass #input_retype_pass").val();
	return (pass1 == pass2);
}

function settingsCtrl_contructor(){
}

function settingsCtrl_destructor(){
}

function settingsCtrl_init(){
	$("#mysettings #inline").prettyPhoto({keyboard_shortcuts: false, default_width: 300, changepicturecallback : function(){
		$("#form_changepass form").validationEngine({scroll : false, unbindEngine : false});
	}, callback : function(){
		$.validationEngine.closePrompt("#form_changepass form");
	}});
}

function settingsCtrl_processSubmit(){
	var oldpass = $(".pp_pic_holder #form_changepass #input_oldpass").val();
	var pass1 = $(".pp_pic_holder #form_changepass #input_newpass").val();
	var pass2 = $(".pp_pic_holder #form_changepass #input_retype_pass").val();
	if (!isValidPassword(oldpass) || !isValidPassword(pass1) || pass1 != pass2) return;
	$("#form_changepass #button_submit").attr("href", "javascript:void(0);");
	$.validationEngine.buildPrompt(".pp_pic_holder #form_changepass #button_submit", "正在发送修改请求，请稍候...", "load");
	$.post("/login/getLoginCode", function(data){
		var password = hex_sha512(data + hex_sha512(oldpass));
		$.post("/login/changePassword", {password : password, newpass : pass1}, function(data){
			if (data == "success"){
				$.validationEngine.buildPrompt(".pp_pic_holder #form_changepass #button_submit", "修改成功！", "pass");
				$("#form_changepass #button_submit").attr("href", "javascript:settingsCtrl.processSubmit();");
			}  else if (data == "wrong password"){
				$.validationEngine.closePrompt(".pp_pic_holder #form_changepass #button_submit");
				$.validationEngine.buildPrompt(".pp_pic_holder #form_changepass #input_oldpass", "错误的密码，请重新输入。", "error");
				$("#form_changepass #button_submit").attr("href", "javascript:settingsCtrl.processSubmit();");
			}  else {
				$.validationEngine.closePrompt(".pp_pic_holder #form_changepass #button_submit");
				$.validationEngine.buildPrompt(".pp_pic_holder #form_changepass #button_submit", "修改失败。", "error");
				$("#form_changepass #button_submit").attr("href", "javascript:settingsCtrl.processSubmit();");
			}
		});
	});
}

if (dojo.getObject("settingsCtrl") == null) var settingsCtrl = new SettingsCtrl();
$(document).ready(function(){
	settingsCtrl.init();
});
